//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2023-05-30  16-22-52 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2023-05-30  16-22-52 
//===============================





using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using System.Diagnostics;
using System.Reflection;

namespace ZS.U3D.BASE.Runtime.Utils
{
    public static class Helper
    {

        #region 文件

        /// <summary>
        /// 文件是否存在
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool HasFile(string path) 
        { 
            return File.Exists(path); 
        }

        /// <summary>
        /// 目录是否存在
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool HasDirectory(string path) 
        { 
            return Directory.Exists(path); 
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="path"></param>
        public static void CreateDirectory(string path)
        {
            try 
            { 
                Directory.CreateDirectory(path); 
            }
            catch (Exception e) 
            { 
                MyDebug.LogError(e.Message); 
            }
        }

        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="path"></param>
        /// <param name="recursive">递归</param>
        /// <returns></returns>
        public static bool DeleteDirectory(string path, bool recursive = true)
        {
            bool result = false;
            try
            {
                if (Directory.Exists(path) == true)
                {
                    Directory.Delete(path, recursive);
                }
                result = true;
            }
            catch (Exception e) 
            { 
                MyDebug.LogError(e.Message); 
            }
            return result;
        }


        #endregion

        #region MD5值

        /// <summary>
        /// 获取文件的MD5值
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string FileMD5(string path) {
            if (File.Exists(path))
            {
                try
                {
                    FileStream fs = new FileStream(path, FileMode.Open);
                    {
                        MD5 md5 = new MD5CryptoServiceProvider();
                        byte[] bytes = md5.ComputeHash(fs);
                        fs.Close();
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            sb.Append(bytes[i].ToString("x2"));
                        }
                        return sb.ToString();
                    }
                }
                catch
                {
                    return string.Empty;
                }

            }
            return string.Empty;
        }

        /// <summary>
        /// 字符串转MD5值
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string StringToMD5(string str) {
            byte[] bytes = Encoding.Default.GetBytes(str);
            return BytesToMD5(bytes);
        }

        /// <summary>
        /// 字节数组转MD5值
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string BytesToMD5(byte[] bytes)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] returnValue = md5.ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < returnValue.Length; i++)
            {
                sb.Append(returnValue[i].ToString("x2"));
            }
            return sb.ToString();
        }

        #endregion

        #region Unity

        /// <summary>
        /// 查找子节点对象
        /// 内部使用“递归算法”
        /// </summary>
        /// <param name="goParent">父对象</param>
        /// <param name="chiildName">查找的子对象名称</param>
        /// <returns></returns>
        public static Transform Find(GameObject goParent, string chiildName)
        {
            Transform searchTrans = goParent.transform.Find(chiildName);
            if (searchTrans == null)
            {
                foreach (Transform trans in goParent.transform)
                {
                    searchTrans = Find(trans.gameObject, chiildName);
                    if (searchTrans != null)
                    {
                        return searchTrans;
                    }
                }
            }
            return searchTrans;
        }

        /// <summary>
        /// 获取子节点（对象）脚本
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="goParent">父对象</param>
        /// <param name="childName">子对象名称</param>
        /// <returns></returns>
        public static T Find<T>(GameObject goParent, string childName) where T : Component
        {
            Transform searchTranformNode = Find(goParent, childName);

            if (searchTranformNode != null)
            {
                return searchTranformNode.gameObject.GetComponent<T>();
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region

        /// <summary>
        /// 2以下的数字无实际意义
        /// </summary>
        /// <param name="frameIndex"></param>
        public static void DebugCallMethod(int frameIndex = 2)
        {
            // 获取调用堆栈信息
            StackTrace stackTrace = new StackTrace();

            // 获取调用当前方法的方法
            StackFrame callerFrame = stackTrace.GetFrame(frameIndex);
            MethodBase callerMethod = callerFrame.GetMethod();

            // 打印调用方法的信息
            MyDebug.Log($"调用当前方法的方法名：{callerMethod.Name} \n 调用当前方法的类名：{callerMethod.DeclaringType?.Name}");
        }

        #endregion
    }


}

